package com.cxl.parser;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

//公共方法的实现类，其他类要想自己做自己的事情就 独自去实现
public abstract class Grab implements InfoGrab<String>,GrabFilter,PtypeFilter {
	static String static_url = null;
	static Parser myParser = new MyParser();
	public String encode = "UTF-8";
	public int searchMaxNum=-1;//设置搜索最大数量   -1 表示不限制  其他数字表示搜索的数量

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}

	public Parser getParser(String url) {

		// if (!url.equals(static_url)) {
		try {
			myParser = new MyParser(url);
		} catch (ParserException e) {

		}
		// static_url = url;
		// }
		// System.out.println(myParser);
		return myParser;
	}

	public Double getPrice(String url) {
		Double db = new Double(0);
		StringBuffer text = new StringBuffer();
		try {
			NodeFilter filter = getFilter();
			NodeList list = getNodesByFilter(url, filter, 1);
			if (list.size() > 0) {
				Node textnode = list.elementAt(0);
				String moneyStr = text.append(
						new String(textnode.toPlainTextString()
								.getBytes(encode))).toString();
				db = getMoney(moneyStr);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return db;
	}

	public String getImgHref(String url) {
		StringBuffer text = new StringBuffer();
		NodeFilter filter = getPictureFilter();
		NodeList list = getNodesByFilter(url, filter, 1);

		if (list.size() > 0) {
			Node textnode = list.elementAt(0);
			if (textnode instanceof ImageTag) {
				{
					text.append(((ImageTag) textnode).getImageURL());
				}
			}
		}
		return text.toString();
	}

	public List<String> searchGoodsUrl(String url) {
		List<String> list = new ArrayList<String>();
	
			NodeFilter filter = getSearchUrlFiter();
			
			NodeList  listnode = getNodesByFilter(url, filter, -1);

			if (listnode.size() > 0) {
				for (int i = 0; i < (listnode.size()); i++) {
					Node textnode = listnode.elementAt(i);
					Tag a = (Tag) textnode;
					//System.out.println(a.getAttribute("href"));
					list.add(a.getAttribute("href"));
				}
			}
			
		return list;
	}

	/**
	 * 正则处理汉字转为数字
	 * 
	 * @param str
	 * @return
	 */
	public Double getMoney(String str) {
		Pattern p = Pattern.compile("\\d+(.\\d{3})*");
		Matcher m = p.matcher(str);
		Double db = new Double(0);
		if (m.find()) {
			db = new Double(m.group());
		}
		return db;
	}

	/**
	 * 获得下一页URL
	 */
	public String getNextUrl(String url) {
		NodeList nodelist = getNodesByFilter(url, getNextFilter(), 1);
		if (nodelist.size() == 0)
			return null;
		Tag a = (Tag) nodelist.elementAt(0);
		return a.getAttribute("href");
	}

	/**
	 * 根据过滤器，node大小 获得相关需要的节点信息
	 * 
	 * @param url
	 * @param filter
	 * @param num
	 *            -1:根据node大小返回;1:返回1个node;other:返回指定数目的 前提是other数量不能大于实际size
	 * @return
	 */
	public NodeList getNodesByFilter(String url, NodeFilter filter, int num) {
		NodeList list = new NodeList();

		Node textnode = null;
		try {
			Parser myParser = getParser(url);
			myParser.setEncoding(getEncode());
			NodeList nodes = myParser.extractAllNodesThatMatch(filter );
		
			int size = nodes.size();

			if (nodes != null && nodes.size() > 0) {
				switch (num) {
				case 1:
					size = 1;// 返回一个
					break; 

				case -1:
					// node实际大小
					break; 

				default:
					size = num > size ? size : num;
					break;
				}

				for (int i = 0; i < size; i++) {
					textnode = (Node) nodes.elementAt(i);
					list.add(textnode);
				}
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}

		return list;
	}

	/**
	 * 获得指定node对应搜索的filter 搜索的 nodelist
	 * @param node
	 * @param filter
	 * @return
	 */
	public NodeList getNextNodeListByFilter(Node node,NodeFilter filter)
	{
			NodeList nodelist=new NodeList();
			node.collectInto(nodelist,filter);
			return nodelist;
	}
	
	
	
	public String getTitle(String url) {
		
		StringBuffer title = new StringBuffer();
		try {
			NodeFilter filter =getTitleFilter();
			NodeList list = getNodesByFilter(url, filter, 1);
			if (list.size() > 0) {
				Node textnode = list.elementAt(0);
				title.append(
						new String(textnode.toPlainTextString()
								.getBytes("UTF-8"))).toString();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return title.toString();
	}

	public String getContent(String url) {
		StringBuffer bufStr=new StringBuffer();
		
		NodeList nodelist = getNodesByFilter(url, getContentFilter(), 1);
		if (nodelist.size() == 0)
			return null;
		bufStr.append("<div>"+nodelist.elementAt(0).toHtml()+"</div>");
		
		nodelist = getNodesByFilter(url, getContentFilter1(), 1);
		if (nodelist.size() == 0)
			return null;
		bufStr.append("<div>"+nodelist.elementAt(0).toHtml()+"</div>");
		return bufStr.toString();
	}

	public List getAllNextGoodsUrl(String url)
	{
		
		List search = new ArrayList();
		
		//得到下一页url地址
		String next=getNextUrl(url);
		
		String reg ="[\n-\r]";
		   Pattern p = Pattern.compile(reg);
		   Matcher m = p.matcher(next);
		   next = m.replaceAll("");

	
		//得到本页商品地址
		List pagelist=searchGoodsUrl(url);
		search.addAll(pagelist);
		
		//继续搜索
		while(next!=null)
		{
			List temp=searchGoodsUrl(next);
			search.addAll(temp);
			if(searchMaxNum!=-1&&search.size()>searchMaxNum)
			{
				return search.subList(0,searchMaxNum);
			}
			next = getNextUrl(next);
			if(next!=null)
			{
			  m = p.matcher(next);
			  next = m.replaceAll("");
			}
			
		}
		return search;
		
	}
	
	public String getHtmlCode(String url) {
		String htmlCode=null;
	 try {
//		 url="http://search.dangdang.com/search.php?key=%C3%C0%B5%C4%20PCJ405&kl=%B0%D9%BB%F5&kl_id=&stop=1";
		HttpClient httpClient = new DefaultHttpClient();
		HttpUriRequest request = new HttpGet(url);
		HttpResponse response;
		
			response = httpClient.execute(request);
		
		HttpEntity entity = response.getEntity();
		htmlCode= EntityUtils.toString(entity);
		System.out.println(htmlCode);
	 }catch(Exception ex)
	 {
		 ex.printStackTrace();
	 }
		return htmlCode;
	}
	
	
	public boolean isExists(String url) {
		NodeList nodelist = getNodesByFilter(url, getNotExistsFilter(), -1);
		if(nodelist.size()>1)
		return false;//不存在
		else
		{
			if(StringUtils.isEmpty(getTitle(url)))
					{
						//名称不存在 
				return false;
					};
		}
		return true;//存在
	}
	
	
	
	
	public String getBrand(String url) {
		NodeList list=getNodesByFilter(url,getBrandFilter(),-1);
		StringBuffer title = new StringBuffer();
		if (list.size() > 0) {
			Node textnode = list.elementAt(0);
			try {
				title.append(
						new String(textnode.toPlainTextString()
								.getBytes("UTF-8"))).toString();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return title.toString();
	}

	public String getXingHao(String url) {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPtypeFirstFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPtypeSecondFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPtypeThirdFilter() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public NodeFilter getPtypeOneFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPtypeThreeFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPtypeTwoFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public static void main(String[] args) {
		
//		Grab amazon=new GrabTaoBao();
//		String url="http://tabc.taobao.com/?search=y";
//		System.out.println(amazon.getContent(url));
		
		 Grab grab=new GrabAmazon();
		 grab.getHtmlCode("http://www.360buy.com/product/371156.html");
		 
		 
//		 List list=grab.searchGoodsUrl("http://www.amazon.cn/s/ref=nb_sb_noss?field-keywords=%E6%A0%BC%E5%85%B0%E4%BB%95+CH2122#/ref=nb_sb_noss?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E6%A0%BC%E5%85%B0%E4%BB%95+CH2122&rh=i%3Aaps%2Ck%3A%E6%A0%BC%E5%85%B0%E4%BB%95+CH2122"); 
// System.out.println(list.get(0).toString());
		//String context= grab.getContent("http://www.360buy.com/product/258811.html");
		
		 
//		List search = grab.getAllNextGoodsUrl("http://shop58612784.taobao.com/?search=y");
//		System.out.println(search.size());
////
//		for (int i = 0; i < search.size(); i++) {
//			System.out.println(search.get(i));
//		}
//		System.out.println("--------------------");

	}

}
